//
//  AXFTabBarController.m
//  爱鲜蜂
//
//  Created by HH on 16/11/7.
//  Copyright © 2016年 胡浩. All rights reserved.
//

#import "AXFTabBarController.h"
#import "AXFNavigationController.h"
#import "AXFModelManager.h"
#import "AXFDetailsModel.h"

@interface AXFTabBarController ()

@end

@implementation AXFTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
	
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToHome:) name:@"pushToHome" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNumberOperation) name:@"addNumber" object:nil];
    //  监听闪送超市的点击减号按钮的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(minusNumberOperation) name:@"minusNumber" object:nil];
}


- (void)addNumberOperation {
    
    NSInteger count = self.viewControllers[2].tabBarItem.badgeValue.integerValue;
    
    count += 1;
    if (count != 0) {
        self.viewControllers[2].tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd", count] ;
    }else{
        self.viewControllers[2].tabBarItem.badgeValue = nil;
    }
    
}


- (void)minusNumberOperation {
    
    NSInteger count = self.viewControllers[2].tabBarItem.badgeValue.integerValue;
    
    count -= 1;
    if (count != 0) {
        self.viewControllers[2].tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd", count] ;
    }else{
        self.viewControllers[2].tabBarItem.badgeValue = nil;
    }
}




- (void)setupUI
{
    self.tabBar.tintColor = [UIColor grayColor];
    
    NSArray<NSDictionary *> *dictArr =
    @[@{@"className":@"AXFHomeController",@"imageName":@"v2_home",@"title":@"首页"},
    @{@"className":@"AXFFlashSupermarketController",@"imageName":@"v2_order",@"title":@"闪电超市"},
    @{@"className":@"AXFShoppingCartController",@"imageName":@"shopCart",@"title":@"购物车"},
    @{@"className":@"AXFMineController",@"imageName":@"v2_my",@"title":@"我的"}];
    
    [dictArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self createControllerWithName:obj[@"className"] imageName:obj[@"imageName"] andtitle:obj[@"title"]];
    }];
    
    
}

#pragma mark - 创建控制器
- (void)createControllerWithName:(NSString *)controllerName imageName:(NSString *)imageName andtitle:(NSString *)title
{
    Class clz = NSClassFromString(controllerName);
    
    UIViewController *controller = [[clz alloc] init];
    controller.title = title;
    controller.tabBarItem.image = [UIImage imageNamed:imageName];
    controller.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]];
    AXFNavigationController *navController = [[AXFNavigationController alloc] initWithRootViewController:controller];
    
    [self addChildViewController:navController];
}

//- (void)pushToHome:(NSNotification *)notification {
//    
//	self.selectedIndex = 0;
//}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"pushToHome" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"addNumber" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"minusNumber" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
